******************************************************************************************
* Name: 	classify-occ.do
* Purpose: 	This file classifies ballot designations into occupation categories using the
*			2010 Census occupation codes and 2016 texts from 
*		    https://www.census.gov/topics/employment/industry-occupation/guidance/indexes.html
******************************************************************************************

preserve

* Insheet 2016 Census text to code crosswalk
insheet using census-occ2016.csv, comma clear

replace text = upper(text)

duplicates drop
keep if text!=""
keep if code!=.

unique code
unique text

* Some duplicates in text
duplicates report text 
duplicates tag text, g(t)
tab t

* if there are multiple codes for a given text description
* choose the lowest code within a text category
bys text (code): keep if _n==1

drop t

duplicates report text 

sort text
gen occid = _n

save census-occ2016, replace

restore

* Make all caps
forval x=1995/2014 {
	replace occ`x' = upper(occ`x')
}

* If multiple occupations listed, separate these
forval x=1995/2014 {
	gen occ`x'_1 = substr(occ`x', 1, strpos(occ`x', "/") -1 )
	replace occ`x'_1 = occ`x' if occ`x'_1==""
		
	gen left = substr(occ`x', strpos(occ`x', "/") + 1, . )
	gen occ`x'_2 = substr(left, 1, strpos(left, "/") -1 )
	replace occ`x'_2 = left if occ`x'_2=="" & left!=occ`x'_1
	
	replace left = substr(left, strpos(left, "/") + 1, . )	
	gen occ`x'_3 = substr(left, 1, strpos(left, "/") -1)	
	replace occ`x'_3 = left if occ`x'_3=="" & left!=occ`x'_1 & left!=occ`x'_2
	
	drop left
}

/* Categories 

3 - Teacher/Educator
4 - Business owner
5 - Law enforcement
6 - Local government employee
7 - Manager
8 - Lawyer, doctor
9 - Finance
10 - Health
11 - Professional (engineer, urban planner)
12 - Agriculture
13 - Student
14 - Services
15 - Sales
16 - Arts, entertainment, sports, media
17 - Community, social service
18 - Installation, repair, construction
19 - Office/admin
20 - Transportation
21 - Business specialists
22 - Self employed, not specified
23 - Production

44444 - Parent
55555 - Retired anything
88888 - Incumbent
77777 - Local/state officeholder
99999 - Not listed

*/

forval x=1995/2014 {
	
	forval y=1/3 {

		* clean up text
		replace occ`x'_`y' = trim(occ`x'_`y')
		replace occ`x'_`y' = subinstr(occ`x'_`y', ".", "", .)
		replace occ`x'_`y' = subinstr(occ`x'_`y', ",", "", .)
		replace occ`x'_`y' = subinstr(occ`x'_`y', "'", "", .)
		replace occ`x'_`y' = subinstr(occ`x'_`y', "-", " ", .)
		replace occ`x'_`y' = subinstr(occ`x'_`y', "ACTING", "", 1)
		replace occ`x'_`y' = subinstr(occ`x'_`y', "ADJUNCT", "", 1)
		replace occ`x'_`y' = subinstr(occ`x'_`y', "MANGER", "MANAGER", 1)
		
		replace occ`x'_`y' = subinstr(occ`x'_`y', "HS ", "HIGH SCHOOL ", 1)
		replace occ`x'_`y' = "VOLLUNTEER" if strpos(occ`x'_`y', "VOLUNTEER")!=0
		replace occ`x'_`y' = "TEACHER" if strpos(occ`x'_`y', "TEACHER")!=0 & strpos(occ`x'_`y', "COLLEGE")==0 & strpos(occ`x'_`y', "AIDE")==0 & strpos(occ`x'_`y', "YOGA")==0  & strpos(occ`x'_`y', "ASSISTANT")==0 

		gen code`x'_`y' = .

		* Retirees
		gen retired`x'_`y' = 1 if strpos(occ`x'_`y', "RETIRED")!=0  | strpos(occ`x'_`y', "RETIRE")!=0  | strpos(occ`x'_`y', "RET ")!=0 | strpos(occ`x'_`y', "SOCIAL SECURITY RECIPIENT")!=0

		* Parental status
		gen parent`x'_`y' = (strpos(occ`x'_`y', "PARENT")!=0 | /// 
							strpos(occ`x'_`y', "MOTHER")!=0 | /// 
							strpos(occ`x'_`y', "FATHER")!=0 | ///
							strpos(occ`x'_`y', "MOM")!=0 | ///
							strpos(occ`x'_`y', "DAD")!=0 | ///
							strpos(occ`x'_`y', "HOMEMAKER")!=0 | ///
							strpos(occ`x'_`y', "HOUSEWIFE")!=0 | ///
							strpos(occ`x'_`y', "HOME MAKER")!=0 | ///
							strpos(occ`x'_`y', "GRANDMOTHER")!=0 | ///
							strpos(occ`x'_`y', "GRANDPARENT")!=0) &  ///
							strpos(occ`x'_`y', "PARENTS")!=1 
		
		* Local/state officeholder
		replace code`x'_`y' = 77777 if strpos(occ`x'_`y', "MEMBER")!=0 & ///
									   strpos(occ`x'_`y', "CREW")==0
		replace code`x'_`y' = 77777 if strpos(occ`x'_`y', "APPOINTED")!=0 | ///
									   strpos(occ`x'_`y', "APPOINTEE")!=0 | ///
									   strpos(occ`x'_`y', "APPOINTMENT INCUMBENT")!=0
		replace code`x'_`y' = 77777 if strpos(occ`x'_`y', "CITY COUNCIL")!=0
		replace code`x'_`y' = 77777 if strpos(occ`x'_`y', "CITY TREASURER")!=0 | ///
									   strpos(occ`x'_`y', "TREASURER")!=0 | ///
									   strpos(occ`x'_`y', "CITY TREAS")!=0 | ///
									   strpos(occ`x'_`y', "COUNTY TREAS")!=0
		replace code`x'_`y' = 77777 if strpos(occ`x'_`y', "MAYOR")!=0 | ///
									   strpos(occ`x'_`y', "MAYO OF")!=0
		replace code`x'_`y' = 77777 if strpos(occ`x'_`y', "COUNCIL")!=0
		replace code`x'_`y' = 77777 if strpos(occ`x'_`y', "ASSEMBLYMAN")!=0 | ///
									   strpos(occ`x'_`y', "ASSEMBLYWOMAN")!=0 | ///
									   strpos(occ`x'_`y', "ASSEMBLYMEN")!=0 
		replace code`x'_`y' = 77777 if ( strpos(occ`x'_`y', "LAWMAKER")!=0 | ///
									   strpos(occ`x'_`y', "LEGISLATOR")!=0 | ///
									   strpos(occ`x'_`y', "SENATOR")!=0 | ///
									   strpos(occ`x'_`y', "STATE ASSEMBLY REP")!=0 ) & ///
									   strpos(occ`x'_`y', "SENATORS")==0 & ///
									   strpos(occ`x'_`y', "SENATOR'S")==0 & ///
									   strpos(occ`x'_`y', "LEGISLATORS")==0 & ///
									   strpos(occ`x'_`y', "LEGISLATOR'S")==0
		replace code`x'_`y' = 77777 if strpos(occ`x'_`y', "JUDGE")!=0 
		replace code`x'_`y' = 77777 if strpos(occ`x'_`y', "SCHOOL DISTRICT SUPERINTENDENT")!=0 
		replace code`x'_`y' = 77777 if strpos(occ`x'_`y', "DISTRICT SUPERINTENDENT")!=0 
		replace code`x'_`y' = 77777 if strpos(occ`x'_`y', "SUPERINTENDENT OF SCHOOLS")!=0 
		replace code`x'_`y' = 77777 if strpos(occ`x'_`y', "BOARD PRESIDENT")!=0 & ///
									   strpos(occ`x'_`y', "PARK BOARD PRESIDENT")==0
		replace code`x'_`y' = 77777 if strpos(occ`x'_`y', "BOARD OF ED")!=0 | ///
									   strpos(occ`x'_`y', "BOARD MEMBER")!=0  | ///
									   strpos(occ`x'_`y', "BOARDMEMBER")!=0  | ///
									   strpos(occ`x'_`y', "COMMUNITY COLLEGE BOARD")!=0 | ///
									   strpos(occ`x'_`y', "GOVERNING BOARD")!=0  | ///
									   strpos(occ`x'_`y', "GOV BRD")!=0  | ///
									   strpos(occ`x'_`y', "GOV BOARD")!=0 | ///
									   strpos(occ`x'_`y', "GOV BD")!=0  | ///
									   strpos(occ`x'_`y', "DISTRICT BOARD")!=0  | ///
									   strpos(occ`x'_`y', "DISTRICT TRUST")!=0  | ///
									   strpos(occ`x'_`y', "SEAT #")!=0 | ///
									   strpos(occ`x'_`y', "SCHOOL BOARD")!=0
		replace code`x'_`y' = 77777 if strpos(occ`x'_`y', "PRESIDENT")!=0 & ///
									   strpos(occ`x'_`y', "SCHOOL BOARD")!=0    
		replace code`x'_`y' = 77777 if strpos(occ`x'_`y', "DISTRICT ATTORNEY")!=0 & ///
									   strpos(occ`x'_`y', "ASSISTANT")==0 & ///
									   strpos(occ`x'_`y', "INVESTIGATOR")==0 & ///
									   strpos(occ`x'_`y', "SENIOR")==0				
		replace code`x'_`y' = 77777 if strpos(occ`x'_`y', "BOARD OF SUPERVISOR")!=0 | ///
									   strpos(occ`x'_`y', "DISTRICT SUPERVISOR")!=0 
		replace code`x'_`y' = 77777 if strpos(occ`x'_`y', "COUNTY SUPERVISOR")!=0 & ///
									   strpos(occ`x'_`y', "AIDE")==0 & ///
									   strpos(occ`x'_`y', "CHIEF")==0 
		replace code`x'_`y' = 77777 if strpos(occ`x'_`y', "ATTORNEY GENERAL")!=0 	
		replace code`x'_`y' = 77777 if strpos(occ`x'_`y', "SUPERVISOR")!=0 & ///
									   strpos(occ`x'_`y', "DISTRICT")!=0 & ///
									   strpos(occ`x'_`y', "TEAM")==0 
		replace code`x'_`y' = 77777 if strpos(occ`x'_`y', "TRUSTEE")!=0 & ///
									   strpos(occ`x'_`y', "FOUNDATION")==0 & ///
									   strpos(occ`x'_`y', "PUBLIC LIBRARY")==0 & ///
									   strpos(occ`x'_`y', "PROVISIONAL")==0

		* Teacher/educator
		replace code`x'_`y' = 3 if strpos(occ`x'_`y', "TEACHER")!=0 | ///
								  strpos(occ`x'_`y', "EDUCATOR")!=0 | ///
								  strpos(occ`x'_`y', "MIDDLE SCHOOL PRINCIPAL")!=0 | ///
								  strpos(occ`x'_`y', "INSTRUCTOR")!=0 | ///
								  strpos(occ`x'_`y', "COLLEGE LECTURER")!=0 | ///
								  strpos(occ`x'_`y', "PROFESSOR")!=0 | ///
								  strpos(occ`x'_`y', "COMMUNITY COLLEGE INSTRUCTOR")!=0 | ///
								  strpos(occ`x'_`y', "CORRECTIONAL EDUCATION")!=0 | ///
								  occ`x'_`y'=="EDUCATION" | ///
								  strpos(occ`x'_`y', "PRINCIPAL")!=0 | ///
								  occ`x'_`y'=="UNIVERSITY LECTURER" | ///
								  occ`x'_`y'=="MARKETING/ECONOMICS LECTURER"
		replace code`x'_`y' = 3 if occ`x'_`y'=="ELEMENTARY SCHOOL EMPLOYEE" | ///
								  strpos(occ`x'_`y', "INSTRUCTIONAL ASSISTANT")!=0 | ///
								  occ`x'_`y'=="SCHOOL DISTRICT EMPLOYEE" | ///
								  occ`x'_`y'=="SCHOOL EMPLOYEE" | ///
								  occ`x'_`y'=="SCHOOL LIAISON" | /// 
								  strpos(occ`x'_`y', "EDUCATION AIDE")!=0 | ///
								  strpos(occ`x'_`y', "EDUCATIONAL AIDE")!=0 | ///
								  strpos(occ`x'_`y', "INSTRUCTIONAL AIDE")!=0 | ///
								  strpos(occ`x'_`y', "LIBRARIAN")!=0 | ///
								  strpos(occ`x'_`y', "CLASSROOM AIDE")!=0 | /// 
								  strpos(occ`x'_`y', "CLASSROOM ASSISTANT")!=0 | ///
								  strpos(occ`x'_`y', "FACULTY")!=0 | ///
								  strpos(occ`x'_`y', "SPECIAL ED")!=0
		* Business owner
		replace code`x'_`y' = 4 if strpos(occ`x'_`y', "BUSINESSMAN")!=0 | ///
								  strpos(occ`x'_`y', "BUSINESSWOMAN")!=0 | /// 
								  strpos(occ`x'_`y', "BUSINESS OWNER")!=0 | ///
								  strpos(occ`x'_`y', "BUSINESS WOMAN")!=0 | ///
								  strpos(occ`x'_`y', "BUSINESS PERSON")!=0 | ///
								  strpos(occ`x'_`y', "BUSINESS MAN")!=0 | ///
								  strpos(occ`x'_`y', "BUSINESSPERSON")!=0 | ///
								  strpos(occ`x'_`y', "SMALL BUSINESS")!=0 | ///
								  strpos(occ`x'_`y', "BOOK STORE OWNER")!=0 | ///
								  strpos(occ`x'_`y', "CONSTRUCTION TRUCKING OWNER")!=0 | ///
								  strpos(occ`x'_`y', "BUINESSMAN")!=0 | ///
								  strpos(occ`x'_`y', "BUISNESSWOMAN") | ///
								  strpos(occ`x'_`y', "BUSINESMAN") | ///
								  occ`x'_`y'=="BUSINESS" | ///
								  occ`x'_`y'=="BUSINESS PROFESSIONAL"
		replace code`x'_`y' = 4 if strpos(occ`x'_`y', "RETAIL NURSERY CO-OWNER")!=0 | ///
								  strpos(occ`x'_`y', "RETAILER")!=0
		replace code`x'_`y' = 4 if strpos(occ`x'_`y', "GENERAL CONTRACTOR")!=0 | ///
								   strpos(occ`x'_`y', "CABINET CONTRACTOR")!=0 | ///
								   strpos(occ`x'_`y', "BUILDING CONTRACTOR")!=0 | ///
								   strpos(occ`x'_`y', "CABINET MAKER")!=0
		replace code`x'_`y' = 4 if strpos(occ`x'_`y', "ENTREPRENEUR")!=0 | ///
								   strpos(occ`x'_`y', "BOOKSELLER")!=0 | /// 
								   occ`x'_`y'=="OWNER" | ///
								   occ`x'_`y'=="ALZHEIMER'S FACILITIES OWNER" | ///
								   occ`x'_`y'=="DESIGN FIRM OWNER" | ///
								   strpos(occ`x'_`y', "FAST FOOD FRANCHISEE")!=0 | ///
								   strpos(occ`x'_`y', "RESTAURANT OWNER")!=0
		replace code`x'_`y' = 4 if strpos(occ`x'_`y', "VINEYARD OWNER")!=0 | ///
							       strpos(occ`x'_`y', "VINTNER")!=0 | ///
								   occ`x'_`y'=="ANTIQUES" | ///
								   occ`x'_`y'=="BARBECUE RESTAURATEUR"
		replace code`x'_`y' = 4 if occ`x'_`y'=="AGRIBUSINESS"
		replace code`x'_`y' = 4 if strpos(occ`x'_`y', "OWNER")!=0 | ///
								   strpos(occ`x'_`y', "BUISNESSMAN")!=0
		* Law enforcement
		replace code`x'_`y' = 5 if strpos(occ`x'_`y', "POLICE")!=0 | ///
								   strpos(occ`x'_`y', "LAW ENFORCEMENT")!=0 | ///
								   strpos(occ`x'_`y', "STATE CORRECTIONAL OFFICER")!=0 | ///
								   strpos(occ`x'_`y', "PEACE OFFICER")!=0 | ///
								   strpos(occ`x'_`y', "STATE SUPERVISING INVESTIGATOR")!=0 | ///
								   strpos(occ`x'_`y', "STATE INVESTIGATOR")!=0 | ///
								   strpos(occ`x'_`y', "SHERIFF")!=0 | ///
								   strpos(occ`x'_`y', "HIGHWAY PATROL")!=0 | ///
								   strpos(occ`x'_`y', "CORRECTIONAL OFFICER")!=0 | ///
								   strpos(occ`x'_`y', "CRIME PREVENTION OFFICER")!=0 | ///
								   strpos(occ`x'_`y', "CRIMINAL INVESTIGATOR")!=0 | ///
								   strpos(occ`x'_`y', "PROBATION OFFICER")!=0 | ///
								   occ`x'_`y'=="CORRECTIONAL LIEUTENANT" | ///
								   occ`x'_`y'=="CORRECTIONAL CAPTAIN" | ///
								   occ`x'_`y'=="CORRECTIONAL PEACE OFFICER" | ///
								   occ`x'_`y'=="CORRECTIONAL SERGEANT" | ///
								   occ`x'_`y'=="CORRECTIONS OFFICER" | ///
								   strpos(occ`x'_`y', "PEACE OFFICER")!=0 | ///
								   strpos(occ`x'_`y', "WARDEN")!=0
		replace code`x'_`y' = 5	if strpos(occ`x'_`y', "PRIVATE INVESTIGATOR")!=0 | /// 
								   strpos(occ`x'_`y', "SHERIFF")!=0	| ///
								   strpos(occ`x'_`y', "PAROLE")!=0 | ///
								   strpos(occ`x'_`y', "FIRE INSPECTOR")!=0
		
		* Local government employee
		replace code`x'_`y' = 6 if strpos(occ`x'_`y', "CITY EMPLOYEE")!=0 | ///
								   strpos(occ`x'_`y', "MUNICIPAL COURT COMMISSIONER")!=0 | ///
								   strpos(occ`x'_`y', "MUNICIPAL CLAIMS")!=0 | ///
								   strpos(occ`x'_`y', "MUNICIPAL CLERICAL")!=0 | ///
								   strpos(occ`x'_`y', "FIRE CAPTAIN")!=0 | ///
								   strpos(occ`x'_`y', "FIRE CHIEF")!=0 | ///
								   strpos(occ`x'_`y', "FIRE COMMUNICATIONS DISPATCHER")!=0 | ///
								   strpos(occ`x'_`y', "FIRE MARSHAL")!=0 | ///
								   strpos(occ`x'_`y', "ASSISTANT ASSESSOR")!=0 | ///
								   strpos(occ`x'_`y', "ASSISTANT IMPERIAL COUNTY COUNSEL")!=0 | ///
								   strpos(occ`x'_`y', "ASSISTANT SUPERINTENDENT")!=0
		replace code`x'_`y' = 6 if strpos(occ`x'_`y', "ASSOCIATE POSTAL SUPERVISOR")!=0 | ///
								   strpos(occ`x'_`y', "CITY PLANNING COMMISSIONER")!=0 | ///
								   strpos(occ`x'_`y', "U. S. POSTAL CARRIER")!=0 | ///
								   strpos(occ`x'_`y', "TRAFFIC OFFICER")!=0 | ///
								   strpos(occ`x'_`y', "PLANNING COMMISSION")!=0 | ///
								   strpos(occ`x'_`y', "CITY COMMISSIONER")!=0 | ///
								   strpos(occ`x'_`y', "CITY INTERDEPARTMENTAL COORDINATOR")!=0 | ///
								   strpos(occ`x'_`y', "CITY PLANNER")!=0 | ///
								   strpos(occ`x'_`y', "CITY REVENUE SUPERVISOR")!=0 | ///
								   strpos(occ`x'_`y', "CIVIL SERVICE EMPLOYEE")!=0 | ///
								   strpos(occ`x'_`y', "COUNTY GOVERNMENT OFFICER")!=0 | ///
								   strpos(occ`x'_`y', "GOVERNMENT EMPLOYEE")!=0 | ///
								   strpos(occ`x'_`y', "FIREFIGHTER")!=0 | ///
								   strpos(occ`x'_`y', "POSTAL CLERK")!=0 | ///
								   strpos(occ`x'_`y', "SUPERIOR COURT COMMISSIONER")!=0 | ///
								   strpos(occ`x'_`y', "COUNTY CLERK")!=0 | ///
								   strpos(occ`x'_`y', "COUNTY CORONER")!=0 | ///
								   strpos(occ`x'_`y', "COUNTY SUPERINTENDENT")!=0 | ///
								   strpos(occ`x'_`y', "COUNTY EMPLOYEE")!=0 | ///
								   strpos(occ`x'_`y', "DEPUTY ASSESSOR")!=0 | ///
								   strpos(occ`x'_`y', "FIRE FIGHTER")!=0 | ///
								   strpos(occ`x'_`y', "STATE EMPLOYEE")!=0 | ///
								   strpos(occ`x'_`y', "PUBLIC EMPLOYEE")!=0 
		replace code`x'_`y' = 6 if strpos(occ`x'_`y', "PARKS COMMISSIONER")!=0 | ///
								   strpos(occ`x'_`y', "PARKS RECREATION COMMISSIONER")!=0 | ///
								   strpos(occ`x'_`y', "PARK/RECREATION DIRECTOR")!=0 | ///
							       strpos(occ`x'_`y', "CODE ENFORCEMENT OFFICER")!=0 | ///
								   strpos(occ`x'_`y', "CODE INSPECTOR")!=0 	
		replace code`x'_`y' = 6 if strpos(occ`x'_`y', "POSTAL")!=0
		replace code`x'_`y' = 6 if strpos(occ`x'_`y', "ASSISTANT")!=0 & ///
								  (strpos(occ`x'_`y', "CLERK")!=0 | strpos(occ`x'_`y', "ASSESSOR")!=0  | strpos(occ`x'_`y', "CONTROLLER")!=0)
		replace code`x'_`y' = 6 if strpos(occ`x'_`y', "LEGISLATIVE")!=0 
		replace code`x'_`y' = 6 if strpos(occ`x'_`y', "GOVERNMENT OFFICER")!=0 | ///
							       strpos(occ`x'_`y', "GOVERNMENT ADVISOR")!=0 
		replace code`x'_`y' = 6 if strpos(occ`x'_`y', "CITY INVESTIGATOR")!=0 
		replace code`x'_`y' = 6 if strpos(occ`x'_`y', "CLERK")!=0
		replace code`x'_`y' = 6 if code`x'_`y'==77777 & strpos(occ`x'_`y', "DEPUTY")!=0
		replace code`x'_`y' = 6 if code`x'_`y'==77777 & strpos(occ`x'_`y', "ASSISTANT")!=0
		replace code`x'_`y' = 6 if strpos(occ`x'_`y', "COMMISSION")!=0
		replace code`x'_`y' = 6 if strpos(occ`x'_`y', "COMISSION")!=0
		replace code`x'_`y' = 6 if strpos(occ`x'_`y', "CIVIL SERVANT")!=0 | strpos(occ`x'_`y', "CIVIL SERVICE")!=0

		* Manager
		replace code`x'_`y' = 7 if strpos(occ`x'_`y', "MANAGER")!=0 | ///
								   strpos(occ`x'_`y', "BUSINESS EXECUTIVE")!=0 | ///
								   strpos(occ`x'_`y', "ADMINISTRATIVE MANGER")!=0 
		replace code`x'_`y' = 7 if strpos(occ`x'_`y', "ENGINEER")!=0 | ///
								   strpos(occ`x'_`y', "COMPUTER")!=0 | ///
								   strpos(occ`x'_`y', "CONSULTANT")!=0 | ///
								   strpos(occ`x'_`y', "ARCHITECT")!=0 | ///
								   occ`x'_`y'=="AEROSPACE" | ///
								   occ`x'_`y'=="AIR TRAFFIC SUPERVISOR" | ///
								   occ`x'_`y'=="AIRPORT SYSTEMS DEVELOPER" | ///
								   occ`x'_`y'=="ANIMATION PRODUCER" | ///
								   occ`x'_`y'=="AUTOMATED SYSTEMS ANALYST" | ///
								   occ`x'_`y'=="BROADCAST EXECUTIVE"
		replace code`x'_`y' = 7 if strpos(occ`x'_`y', "DIRECTOR")!=0 | ///
								   strpos(occ`x'_`y', "ADMINISTRATOR")!=0
		replace code`x'_`y' = 7 if strpos(occ`x'_`y', "CHIEF EXECUTIVE OFFICER")!=0 | ///
								   strpos(occ`x'_`y', "CHIEF OPERATING OFFICER")!=0 | ///
								   strpos(occ`x'_`y', "VICE PRESIDENT")!=0 | ///
								   strpos(occ`x'_`y', "CEO")!=0 | ///
								   occ`x'_`y'=="CORPORATE EXECUTIVE" | ///
								   occ`x'_`y'=="SOFTWARE EXECUTIVE" | ///
								   occ`x'_`y'=="CORPORATION EXECUTIVE OFFICER" | ///
								   occ`x'_`y'=="CORPORATE PRESIDENT" | ///
								   strpos(occ`x'_`y', "COMPANY PRESIDENT")!=0 | ///
								   strpos(occ`x'_`y', "C.E.O.")!=0
		replace code`x'_`y' = 7 if strpos(occ`x'_`y', "ADVERTISING EXECUTIVE")!=0 | ///
								   strpos(occ`x'_`y', "INVESTMENT EXECUTIVE")!=0
		replace code`x'_`y' = 7 if strpos(occ`x'_`y', "EXECUTIVE")!=0 & strpos(occ`x'_`y', "ASSISTANT")==0	
		
		* Lawyer, doctor
		replace code`x'_`y' = 8 if strpos(occ`x'_`y', "ATTORNEY")!=0 & strpos(occ`x'_`y', "DISTRICT")==0
		replace code`x'_`y' = 8 if strpos(occ`x'_`y', "LAWYER")!=0 | ///
								   strpos(occ`x'_`y', "PROSECUTOR")!=0 | ///
								   strpos(occ`x'_`y', "PUBLIC DEFENDER")!=0 | ///
								   strpos(occ`x'_`y', "DEPUTY D.A.")!=0 | ///
								   strpos(occ`x'_`y', "TRIAL COUNSEL")!=0 | ///
								   strpos(occ`x'_`y', "LITIGATOR")!=0 
		replace code`x'_`y' = 8 if strpos(occ`x'_`y', "SURGEON")!=0 | ///
								   occ`x'_`y'=="PEDIATRICIAN" | ///
								   occ`x'_`y'=="PHYSICIAN" | ///
								   occ`x'_`y'=="PHYSICIAN (RADIOLOGIST)" | ///
								   strpos(occ`x'_`y', "OPTOMETRIST")!=0 | ///
								   strpos(occ`x'_`y', "ORTHODONTIST")!=0 | ///
								   strpos(occ`x'_`y', "PODIATRIST")!=0 | ///
								   strpos(occ`x'_`y', "PODIATRIC PHYSICIAN")!=0 | ///
								   strpos(occ`x'_`y', "M.D.")!=0 | ///
								   occ`x'_`y'=="DOCTOR" | ///
								   strpos(occ`x'_`y', "PODIATRIC")!=0 | ///
								   strpos(occ`x'_`y', "OPTOMETRY")!=0 | ///
								   strpos(occ`x'_`y', "DOCTOR OF MEDICINE")!=0 | ///
								   strpos(occ`x'_`y', "CHIROPRACTIC")!=0
		
		* Finance
		replace code`x'_`y' = 9 if strpos(occ`x'_`y', "ACCOUNTANT")!=0 | ///
							       strpos(occ`x'_`y', "FINANCE")!=0 | ///
								   strpos(occ`x'_`y', "FINANCIAL")!=0 | ///
								   strpos(occ`x'_`y', "PRICING ANALYST")!=0 | ///
								   strpos(occ`x'_`y', "BUDGET ANALYST")!=0 | ///
								   strpos(occ`x'_`y', "ACCOUNT EXECUTIVE")!=0 | ///
								   strpos(occ`x'_`y', "STOCK BROKER")!=0 | ///
								   strpos(occ`x'_`y', "STOCK TRADER")!=0 | ///
								   strpos(occ`x'_`y', "MORTGAGE BROKER")!=0 | ///
								   strpos(occ`x'_`y', "MORTGAGE LENDER")!=0 | ///
								   strpos(occ`x'_`y', "AUDITOR")!=0 | ///
								   strpos(occ`x'_`y', "CPA")!=0 | ///
							       strpos(occ`x'_`y', "C.P.A.")!=0 | ///
								   strpos(occ`x'_`y', "C. P. A.")!=0 | ///
								   strpos(occ`x'_`y', "ESCROW OFFICER")!=0 | ///
								   strpos(occ`x'_`y', "REAL ESTATE")!=0
		replace code`x'_`y' = 9 if strpos(occ`x'_`y', "TAX") | strpos(occ`x'_`y', "BROKER") 
		replace code`x'_`y' = 9 if strpos(occ`x'_`y', "CLAIMS ANALYST")!=0 | ///
								   strpos(occ`x'_`y', "CLAIMS REPRESENTATIVE")!=0 | ///
								   strpos(occ`x'_`y', "ADJUSTER")!=0 | ///
								   strpos(occ`x'_`y', "LOAN OFFICER")!=0
		replace code`x'_`y' = 9 if strpos(occ`x'_`y', "BANK")!=0 & ///
								   strpos(occ`x'_`y', "RIVERBANK")==0 & ///
								   strpos(occ`x'_`y', "FOOD BANK")==0 
		
		* Health
		replace code`x'_`y' = 10 if strpos(occ`x'_`y', "NURSE")!=0 | ///
							        strpos(occ`x'_`y', "OCCUPATIONAL THERAPIST")!=0 | ///
									strpos(occ`x'_`y', "DENTAL HYGIENIST")!=0 | ///
								    strpos(occ`x'_`y', "CHIROPRACTOR")!=0 | ///
									strpos(occ`x'_`y', "VETERINARIAN")!=0 | ///
									strpos(occ`x'_`y', "PHYSICAL THERAPIST")!=0 | ///
									strpos(occ`x'_`y', "PHARMACIST")!=0 | ///
									strpos(occ`x'_`y', "HEALTHCARE PROVIDER")!=0 | ///
									strpos(occ`x'_`y', "HEALTHCARE WORKER")!=0 | ///
									strpos(occ`x'_`y', "HEALTH CARE SPECIALIST")!=0 | ///
									strpos(occ`x'_`y', "DENTIST")!=0 | ///
								    strpos(occ`x'_`y', "PHYSICIAN ASSISTANT")!=0 | ///
									strpos(occ`x'_`y', "OPTICIAN")!=0 | ///
									occ`x'_`y'=="HEALTH PROFESSIONAL" | ///
									occ`x'_`y'=="HEALTH CARE PROVIDER" | ///
									strpos(occ`x'_`y', "DENTAL ASSISTANT")!=0 | ///
								    strpos(occ`x'_`y', "DOCTOR OF CHIROPRACTIC")!=0 | ///
									occ`x'_`y'=="EMT" | ///
									strpos(occ`x'_`y', "IN-HOME PROVIDER")!=0 | ///
									occ`x'_`y'=="HEALTH CARE PROFESSIONAL" | ///
									strpos(occ`x'_`y', "PATHOLOGIST")!=0
		
		* Professional 
		replace code`x'_`y' = 11 if strpos(occ`x'_`y', "BIOLOGIST") | ///
								    strpos(occ`x'_`y', "ARCHAEOLOGIST")!=0 | ///
									strpos(occ`x'_`y', "CHEMIST")!=0 | ///
									strpos(occ`x'_`y', "SCIENTIST")!=0 | ///
								    strpos(occ`x'_`y', "RESEARCHER")!=0 | ///
									strpos(occ`x'_`y', "ECONOMIST")!=0 | ///
									strpos(occ`x'_`y', "PSYCHOLOGIST")!=0 | ///
									occ`x'_`y'=="PHILOSOPHER" | ///
									occ`x'_`y'=="LEGAL SCHOLAR" | ///
									strpos(occ`x'_`y', "ECOLOGIST")!=0 | ///
									strpos(occ`x'_`y', "GEOLOGIST")!=0 | ///
									strpos(occ`x'_`y', "MATHEMATICIAN")!=0 | ///
									strpos(occ`x'_`y', "URBAN PLANNER")!=0 | ///
								    strpos(occ`x'_`y', "PHYSICIST")!=0 | ///
									strpos(occ`x'_`y', "LAND USE PLANNER")!=0 | ///
									strpos(occ`x'_`y', "SOCIOLOGIST")!=0 | ///
									strpos(occ`x'_`y', "STATISTICIAN")!=0
		
		* Agriculture
		replace code`x'_`y' = 12 if strpos(occ`x'_`y', "FARMER")!=0 | ///
								    strpos(occ`x'_`y', "RANCHER")!=0 | ///
									strpos(occ`x'_`y', "AGRICULTURALIST")!=0 | ///
									occ`x'_`y'=="AGRICULTURE" | ///
									strpos(occ`x'_`y', "AQUACULTURIST")!=0 | ///
									strpos(occ`x'_`y', "BEEKEEPER")!=0 | ///
									strpos(occ`x'_`y', "CATTLE RANCHING")!=0 | ///
									strpos(occ`x'_`y', "CATTLEMAN")!=0  | ///
									strpos(occ`x'_`y', "ARBORIST")!=0 | ///
									strpos(occ`x'_`y', "CITRUS GROWER")!=0 | ///
									strpos(occ`x'_`y', "DAIRYMAN")!=0 | ///
									strpos(occ`x'_`y', "DAIRY FEEDER")!=0 | ///
									strpos(occ`x'_`y', "FARMING")!=0 | ///
									strpos(occ`x'_`y', "FORESTRY")!=0 | ///
									strpos(occ`x'_`y', "FORESTER")!=0 | ///
									strpos(occ`x'_`y', "GROWER")!=0 | ///
									strpos(occ`x'_`y', "LOGGER")!=0 | ///
									occ`x'_`y'=="AGRARIAN" 

		* Student
		replace code`x'_`y' = 13 if strpos(occ`x'_`y', "STUDENT")!=0 | ///
									occ`x'_`y'=="BUSINESS MAJOR" | ///
									occ`x'_`y'=="DOCTORAL CANDIDATE"
		
		* Services
		replace code`x'_`y' = 14 if strpos(occ`x'_`y', "CASHIER")!=0 | ///
									strpos(occ`x'_`y', "BARBER")!=0 | ///
									strpos(occ`x'_`y', "CAFETERIA WORKER")!=0 | ///
									strpos(occ`x'_`y', "CHILD CARE")!=0 | ///
									strpos(occ`x'_`y', "CHILDCARE")!=0 | ///
									strpos(occ`x'_`y', "YOGA INSTRUCTOR")!=0 | ///
									strpos(occ`x'_`y', "HAIR STYLIST")!=0 | ///
									strpos(occ`x'_`y', "HAIRSTYLIST")!=0 | ///
									strpos(occ`x'_`y', "CUSTOMER SERVICE TELLER")!=0 | ///
									occ`x'_`y'=="DOWNTOWN DELICATESSEN EMPLOYEE" | ///
									occ`x'_`y'=="GROCERY CLERK" | ///
									occ`x'_`y'=="TRAINER" | ///
									occ`x'_`y'=="WAITER" | ///
									occ`x'_`y'=="WAITRESS"
		replace code`x'_`y' = 14 if strpos(occ`x'_`y', "HOME CARE PROVIDER")!=0 | ///
									occ`x'_`y'=="COOK" | ///
									occ`x'_`y'=="BEAUTICIAN" | ///
									occ`x'_`y'=="BAKER" | ///
									occ`x'_`y'=="DAYCARE PROVIDER" | ///
									occ`x'_`y'=="ELDERLY CARE PROVIDER"
		
		* Sales
		replace code`x'_`y' = 15 if strpos(occ`x'_`y', "REAL ESTATE AGENT")!=0 | ///
									strpos(occ`x'_`y', "REAL ESTATE BROKER") | ///
									strpos(occ`x'_`y', "REALTOR")!=0 | ///
									strpos(occ`x'_`y', "REALTOR ASSOCIATE")!=0 
		replace code`x'_`y' = 15 if strpos(occ`x'_`y', "SALES")!=0 | ///
									strpos(occ`x'_`y', "ACCOUNT REPRESENTATIVE")!=0 | ///
									strpos(occ`x'_`y', "INSURANCE AGENT")!=0 | ///
									strpos(occ`x'_`y', "INSURANCE BROKER")!=0
		replace code`x'_`y' = 15 if strpos(occ`x'_`y', "RETAIL CLERK")!=0 | ///
									strpos(occ`x'_`y', "BAKERY CLERK")!=0 | ///
									strpos(occ`x'_`y', "MERCHANDISER")!=0
		replace code`x'_`y' = 15 if strpos(occ`x'_`y', "PHARMACEUTICAL REPRESENTATIVE")!=0 | ///
									strpos(occ`x'_`y', "BUSINESS REPRESENTATIVE")!=0 
		replace code`x'_`y' = 15 if strpos(occ`x'_`y', "MARKETING")!=0 & ///
									strpos(occ`x'_`y', "LECTURER")==0 & ///
									strpos(occ`x'_`y', "ANALYST")==0 & ///
									strpos(occ`x'_`y', "SPECIALIST")==0 
		
		* Arts, entertainment, sports, media
		replace code`x'_`y' = 16 if strpos(occ`x'_`y', "WRITER")!=0 | ///
									strpos(occ`x'_`y', "ARTIST")!=0 | ///
									strpos(occ`x'_`y', "ACTOR")!=0 | ///
									strpos(occ`x'_`y', "AUTHOR")!=0 
		replace code`x'_`y' = 16 if occ`x'_`y'=="FAMILY FILM PRODUCER" | ///
									strpos(occ`x'_`y', "ILLUSTRATOR")!=0 | ///
									strpos(occ`x'_`y', "JOURNALIST")!=0 | ///
									occ`x'_`y'=="PHOTOGRAPHER" | ///
									strpos(occ`x'_`y', "EDITOR")!=0 | ///
									strpos(occ`x'_`y', "PUBLISHER")!=0 | ///
									strpos(occ`x'_`y', "TELEVISION")!=0 | ///
									strpos(occ`x'_`y', "RADIO")!=0 | ///
									strpos(occ`x'_`y', "PRINTER")!=0 | ///
									strpos(occ`x'_`y', "MUSICIAN")!=0 | ///
									strpos(occ`x'_`y', "ACTRESS")!=0
		
		* Community, social service
		replace code`x'_`y' = 17 if strpos(occ`x'_`y', "MINISTER")!=0 | ///
									strpos(occ`x'_`y', "MEDIATOR")!=0 | ///
									strpos(occ`x'_`y', "ARBITRATOR")!=0 | ///
									strpos(occ`x'_`y', "SOCIAL WORKER")!=0 | ///
									strpos(occ`x'_`y', "CLERGY")!=0 | ///
									strpos(occ`x'_`y', "PASTOR")!=0 | ///
									strpos(occ`x'_`y', "CHAPLAIN")!=0 | ///
									strpos(occ`x'_`y', "COUNSELOR")!=0 | ///
									strpos(occ`x'_`y', "COMMUNITY COLLEGE COUNSELOR")!=0 | ///
									occ`x'_`y'=="COUNSELOR" | ///
									occ`x'_`y'=="EDUCATION COUNSELOR" | ///
									occ`x'_`y'=="HEALTH-YOUTH ADVISER" | ///
									strpos(occ`x'_`y', "THERAPIST")!=0 | ///
									strpos(occ`x'_`y', "BISHOP")!=0 | ///
									strpos(occ`x'_`y', "CAREER ADVISOR")!=0 | ///
									occ`x'_`y'=="REVEREND" 
		replace code`x'_`y' = 17 if strpos(occ`x'_`y', "EMPLOYMENT INTERVIEWER")!=0 | ///
									strpos(occ`x'_`y', "EMPLOYMENT PLACEMENT SPECIALIST")!=0 | ///
									occ`x'_`y'=="MENTAL HEALTH REHABILITATOR" | ///
									strpos(occ`x'_`y', "YOUTH")!=0
		replace code`x'_`y' = 17 if occ`x'_`y'=="MARRIAGE/FAMILY THERAPIST" | ///
									occ`x'_`y'=="SCHOOL COMMUNITY LIAISON" | ///
									strpos(occ`x'_`y', "COMMUNITY ORGANIZER")!=0 | ///
									strpos(occ`x'_`y', "COMMUNITY SERVICE")!=0
		replace code`x'_`y' = 17 if occ`x'_`y'=="COMMUNITY EDUCATIONAL LIAISON" | ///
									occ`x'_`y'=="COMMUNITY OUTREACH SPECIALIST" | ///
									strpos(occ`x'_`y', "VOLUNTEER")!=0 | ///
									strpos(occ`x'_`y', "ACTIVIST")!=0 | ///
									strpos(occ`x'_`y', "COMMUNITY AFFAIRS")!=0  | ///
									strpos(occ`x'_`y', "COMMUNITY RELATION")!=0  | ///
									strpos(occ`x'_`y', "COMMUNITY SPECIALIST")!=0  | ///
									strpos(occ`x'_`y', "COMMUNITY SUPPORT")!=0  | ///
									strpos(occ`x'_`y', "COMMUNITY WORKER")!=0  | ///
									strpos(occ`x'_`y', "COMMUNITY ADVISOR")!=0       
		
		* Installation, repair, construction
		replace code`x'_`y' = 18 if strpos(occ`x'_`y', "LABORER")!=0 | ///
									strpos(occ`x'_`y', "AUTO SERVICE TECHNICIAN")!=0 | ///
									strpos(occ`x'_`y', "AUTOMOTIVE AND TOWING")!=0 | ///
									strpos(occ`x'_`y', "BUILDER")!=0 | ///
									strpos(occ`x'_`y', "CARPENTER")!=0 | ///
									strpos(occ`x'_`y', "CEMENT FINISHER")!=0 | ///
									strpos(occ`x'_`y', "GARDENER")!=0 | ///
									strpos(occ`x'_`y', "CONSTRUCTION WORKER")!=0 | ///
									strpos(occ`x'_`y', "CUSTODIAN")!=0 | ///
									strpos(occ`x'_`y', "MECHANIC")!=0 | ///
									occ`x'_`y'=="ELECTRICAL TRANSFORMER TECH." | ///
									strpos(occ`x'_`y', "ELECTRONICS TECHNICIAN")!=0 | ///
									strpos(occ`x'_`y', "EQUIPMENT MECHANIC")!=0 | ///
									strpos(occ`x'_`y', "TECHNICIAN")!=0 | ///
									strpos(occ`x'_`y', "FIRE SPRINKLER FOREMAN")!=0 | ///
									strpos(occ`x'_`y', "PLUMBER")!=0 | ///
									occ`x'_`y'=="AGRICULTURAL FIELD AIDE" | ///
									occ`x'_`y'=="FARM WORKER" | ///
									occ`x'_`y'=="HOUSEKEEPER" | ///
									occ`x'_`y'=="HOUSEKEEPING AIDE" | ///
									strpos(occ`x'_`y', "MAINTENANCE WORKER")!=0 
		replace code`x'_`y' = 18 if strpos(occ`x'_`y', "CRANE OPERATOR")!=0 | ///
									strpos(occ`x'_`y', "MAINTENANCE")!=0 | ///
									strpos(occ`x'_`y', "ROAD WORKER") | ///
									strpos(occ`x'_`y', "CONSTRUCTION") | ///
									strpos(occ`x'_`y', "HANDYMAN")!=0 | ///
									strpos(occ`x'_`y', "ELECTRICIAN")!=0 
		replace code`x'_`y' = 18 if strpos(occ`x'_`y', "CONSTRUCTION")!=0 & code`x'_`y'!=9
		replace code`x'_`y' = 18 if strpos(occ`x'_`y', "INSPECTOR")!=0 & strpos(occ`x'_`y', "FIRE")==0
	
		* Office/admin
		replace code`x'_`y' = 19 if strpos(occ`x'_`y', "BOOKKEEPER")!=0 | ///
									strpos(occ`x'_`y', "ADMINISTRATIVE ASSISTANT")!=0 | ///
									strpos(occ`x'_`y', "ACCOUNT CLERK")!=0 | ///
									occ`x'_`y'=="ADMINISTRATIVE" | ///
									occ`x'_`y'=="ADMINISTRATIVE INTERN" | ///
									strpos(occ`x'_`y', "SECRETARY")!=0 | ///
									strpos(occ`x'_`y', "ATTENDANCE CLERK")!=0  | ///
									strpos(occ`x'_`y', "BOOKKEEPING")!=0 | ///
									strpos(occ`x'_`y', "ADMINISTRATIVE ANALYST")!=0 | ///
									occ`x'_`y'=="AEROSPACE COMMODITIES ADMIN." | ///
									strpos(occ`x'_`y', "OFFICE ASSISTANT")!=0 | ///
									strpos(occ`x'_`y', "CUSTOMER SUPPORT")!=0 | ///
									strpos(occ`x'_`y', "PARALEGAL")!=0 | ///
									strpos(occ`x'_`y', "RECEPTIONIST")!=0 | ///
									strpos(occ`x'_`y', "LEGAL ASSISTANT")!=0 | ///
									occ`x'_`y'=="TRAVEL AGENT" | ///
									strpos(occ`x'_`y', "LIBRARY CLERK")!=0 | ///
									strpos(occ`x'_`y', "LIBRARY ASSISTANT")!=0 | ///
									strpos(occ`x'_`y', "LIBRARY SPECIALIST")!=0 | ///
									strpos(occ`x'_`y', "LIBRARY MEDIA")!=0 | ///
									strpos(occ`x'_`y', "LIBRARY OUTREACH")!=0
		replace code`x'_`y' = 19 if strpos(occ`x'_`y', "EXECUTIVE ASSISTANT")!=0 | ///
									strpos(occ`x'_`y', "DATA ENTRY OPERATOR")!=0 | ///
									strpos(occ`x'_`y', "COORDINATOR")!=0	
		replace code`x'_`y' = 19 if strpos(occ`x'_`y', "ADMINISTRATIVE SUPPORT")!=0	| ///
									strpos(occ`x'_`y', "TYPIST")!=0 	
		
		* Transportation
		replace code`x'_`y' = 20 if strpos(occ`x'_`y', "AIR TRAFFIC CONTROLLER")!=0 | ///
									strpos(occ`x'_`y', "AIRWAY TRANSPORTATION SPECIALIST")!=0 | ///
									strpos(occ`x'_`y', "AVIATOR")!=0 | ///
									strpos(occ`x'_`y', "BUS DRIVER")!=0 | ///
									strpos(occ`x'_`y', "TRUCK DRIVER")!=0 | ///
									strpos(occ`x'_`y', "AIRCRAFT MECHANIC")!=0 | ///
									strpos(occ`x'_`y', "AIRLINE PILOT")!=0 | ///
									strpos(occ`x'_`y', "REPAIRMAN")!=0 | ///
									strpos(occ`x'_`y', "DRIVER")!=0 | ///
									strpos(occ`x'_`y', "DELIVERYMAN")!=0 | ///
									strpos(occ`x'_`y', "AIRCRAFT MAINTENANCE")!=0 | ///
									strpos(occ`x'_`y', "TRUCKER")!=0
		replace code`x'_`y' = 20	if strpos(occ`x'_`y', "BUS ") | strpos(occ`x'_`y', "RAILROAD")

		* Business specialists
		replace code`x'_`y' = 21 if strpos(occ`x'_`y', "APPRAISER")!=0 | ///
									strpos(occ`x'_`y', "ASSESSMENT CLERK")!=0 | ///
									strpos(occ`x'_`y', "AUCTIONEER")!=0 | ///
									strpos(occ`x'_`y', "BUILDING INSPECTOR")!=0 
		replace code`x'_`y' = 21 if strpos(occ`x'_`y', "INVESTIGATOR")!=0 & ///
									(strpos(occ`x'_`y', "FRAUD")!=0 | strpos(occ`x'_`y', "INSURANCE")!=0 | strpos(occ`x'_`y', "REVENUE")!=0)
		replace code`x'_`y' = 21 if strpos(occ`x'_`y', "MARKETING")!=0 & ///
									(strpos(occ`x'_`y', "ANALYST")!=0 | strpos(occ`x'_`y', "SPECIALIST")!=0)
		
		* Self employed, not specified
		replace code`x'_`y' = 22 if occ`x'_`y'=="SELF EMPLOYED" 
		
		* Production	
		replace code`x'_`y' = 23 if strpos(occ`x'_`y', "OPERATOR")!=0 & ///
									(strpos(occ`x'_`y', "WATER")!=0 | ///
									strpos(occ`x'_`y', "ROAD")!=0 | ///
									strpos(occ`x'_`y', "OIL")!=0 | ///
									strpos(occ`x'_`y', "BASIN")!=0 | ///
									strpos(occ`x'_`y', "COTTON GIN")!=0 | ///
									strpos(occ`x'_`y', "EQUIPMENT")!=0 | ///
									strpos(occ`x'_`y', "ENERGY")!=0 | ///
									strpos(occ`x'_`y', "FUEL")!=0 | ///
									strpos(occ`x'_`y', "GEOTHERMAL")!=0 | ///
									strpos(occ`x'_`y', "HYDROELECTRIC")!=0 | ///
									strpos(occ`x'_`y', "MACHINE")!=0 | ///
									strpos(occ`x'_`y', "NUCLEAR")!=0 | ///
									strpos(occ`x'_`y', "ORE SEPARATOR")!=0 | ///
									strpos(occ`x'_`y', "PETROCHEMICAL")!=0 | ///
									strpos(occ`x'_`y', "PLANT")!=0)
		replace code`x'_`y' = 23 if strpos(occ`x'_`y', "MACHINIST")!=0		
		
		* No info
		replace code`x'_`y' = 99999 if occ`x'_`y'=="NO BALLOT DESIGNATION" | ///
									   occ`x'_`y'=="CANDIDATE" | ///
									   occ`x'_`y'=="INFORMATION UNAVAILABLE"
		replace code`x'_`y' = 99999 if strpos(occ`x'_`y', "SCHOOL DISTRICT")!=0 & code`x'_`y'==.
		replace code`x'_`y' = 99999 if strpos(occ`x'_`y', "CITY OF")!=0 & code`x'_`y'==.
		replace code`x'_`y' = 99999 if strpos(occ`x'_`y', "DISTRICT")!=0 & code`x'_`y'==.

		* Incumbent	
		replace code`x'_`y' = 88888 if occ`x'_`y'=="INCUMBENT"

		* Parent/Retired (and no other category)	
		replace code`x'_`y' = 44444 if parent`x'_`y'==1 & code`x'_`y'==.
		replace code`x'_`y' = 55555 if retired`x'_`y'==1
	}
}

save temp.dta, replace  

*****************************************************************
* Compile unique and exhaustive list of occupations across years
*****************************************************************

forval x=1995/2014 {

	forval y=1/3 {
		disp "`x' `y'"
		preserve
		keep occ`x'_`y' code`x'_`y' 
		rename occ`x'_`y' occ
		rename code`x'_`y' code 
		duplicates drop  
		drop if occ==""
		if !(`x'==1995 & `y'==1) append using occ-list.dta
		save occ-list.dta, replace
		restore 
	}
}

use occ-list.dta, clear

duplicates drop  
count

save occ-list.dta, replace

* drop parents, retirees, officeholders, incumbents not stated 
keep if code==. | code<100
drop code 

* Classify remaining occupations by doing reclink merge
gen occ_clean = occ
rename occ_clean text 
sort text
gen id = _n

reclink text using census-occ2016.dta, idmaster(id) idusing(occid) gen(score) 

tab _m

rename text occ_clean

replace code = 66666 if code==.

keep occ occ_clean code 

* In some cases, there's a tie for the best match
* Keep higher code
duplicates drop
bys occ (code): keep if _n==1

gen reclink=1 if code!=66666

save elections-occ-coded.dta, replace

*****************************************************************
* Incorporate into main data
*****************************************************************

use temp.dta, clear 
count

* Use reclink merge unless manual classification>100
forval x=1995/2014 {

	forval y=1/3 {

		rename code`x'_`y' code_orig`x'_`y'
		rename occ`x'_`y' occ
		merge m:1 occ using elections-occ-coded.dta
		tab _m
		drop if _m==2
		drop _m 
		rename occ occ`x'_`y'
		rename code code`x'_`y'
		rename reclink reclink`x'_`y'
		replace code`x'_`y' = code_orig`x'_`y' if code`x'_`y'==.
		
	}
}


* Additional modifications from manual classification
/*
11111 - Student
22222 - Govt employee
33333 - Businessowner
44444 - Parent
55555 - Retired anything
66666 - Not classified 
77777 - Other officeholder, not incumbent
88888 - Incumbent
99999 - Not listed
*/

forval x=1995/2014 {

	forval y=1/3 {
		
		* business owner
		replace code`x'_`y' = 33333 if code_orig`x'_`y'==4
		
		* student
		replace code`x'_`y' = 11111 if code_orig`x'_`y'==13
		
		* government employee
		replace code`x'_`y' = 22222 if code_orig`x'_`y'==6
		
		replace reclink`x'_`y' = . if code`x'_`y'==11111 | code`x'_`y'==22222 | code`x'_`y'==33333

	}
}


* group according to census 2010 grouping: https://usa.ipums.org/usa/volii/occ_acs.shtml
label define occ_group 1 "management"
label define occ_group 2 "business/financial operations", add
label define occ_group 3 "computer/mathematical", add
label define occ_group 4 "architecture/engineering", add
label define occ_group 5 "life/physical/social science", add
label define occ_group 6 "community/social service", add
label define occ_group 7 "legal", add
label define occ_group 8 "education/training/library", add
label define occ_group 9 "arts/design/entertainment/sports/media", add
label define occ_group 10 "healthcare practitioners", add
label define occ_group 11 "healthcare support", add
label define occ_group 12 "protective service", add
label define occ_group 13 "food prep/serving related", add
label define occ_group 14 "building/grounds cleaning and maintenance", add
label define occ_group 15 "personal care/service", add
label define occ_group 16 "sales", add
label define occ_group 17 "office/admin support", add
label define occ_group 18 "farming/fishing/forestry", add
label define occ_group 19 "construction/extraction", add
label define occ_group 20 "installation/maintenance/repair", add
label define occ_group 21 "production", add
label define occ_group 22 "transportation", add
label define occ_group 23 "material moving", add
label define occ_group 24 "military", add
label define occ_group 25 "student", add
label define occ_group 26 "govt employee", add
label define occ_group 27 "business owner", add
label define occ_group 28 "parent/homemaker", add
label define occ_group 29 "retired", add
label define occ_group 30 "not classified", add
label define occ_group 31 "officeholder", add
label define occ_group 32 "incumbent", add
label define occ_group 33 "none listed", add


* Occupational category for first occupation
forval x=1995/2014 {
	
	gen occ_group`x' =     1 if code`x'_1>=10 & code`x'_1<=430
	replace occ_group`x' = 2 if code`x'_1>=500 & code`x'_1<=950
	replace occ_group`x' = 3 if code`x'_1>=1000 & code`x'_1<=1240
	replace occ_group`x' = 4 if code`x'_1>=1300 & code`x'_1<=1560
	replace occ_group`x' = 5 if code`x'_1>=1600 & code`x'_1<=1965
	replace occ_group`x' = 6 if code`x'_1>=2000 & code`x'_1<=2060
	replace occ_group`x' = 7 if code`x'_1>=2100 & code`x'_1<=2160
	replace occ_group`x' = 8 if code`x'_1>=2200 & code`x'_1<=2550
	replace occ_group`x' = 9 if code`x'_1>=2600 & code`x'_1<=2960
	replace occ_group`x' = 10 if code`x'_1>=3000 & code`x'_1<=3540
	replace occ_group`x' = 11 if code`x'_1>=3600 & code`x'_1<=3655
	replace occ_group`x' = 12 if code`x'_1>=3700 & code`x'_1<=3955
	replace occ_group`x' = 13 if code`x'_1>=4000 & code`x'_1<=4160
	replace occ_group`x' = 14 if code`x'_1>=4200 & code`x'_1<=4250
	replace occ_group`x' = 15 if code`x'_1>=4300 & code`x'_1<=4650
	replace occ_group`x' = 16 if code`x'_1>=4700 & code`x'_1<=4965
	replace occ_group`x' = 17 if code`x'_1>=5000 & code`x'_1<=5940
	replace occ_group`x' = 18 if code`x'_1>=6005 & code`x'_1<=6130
	replace occ_group`x' = 19 if code`x'_1>=6200 & code`x'_1<=6940
	replace occ_group`x' = 20 if code`x'_1>=7000 & code`x'_1<=7630
	replace occ_group`x' = 21 if code`x'_1>=7700 & code`x'_1<=8965
	replace occ_group`x' = 22 if code`x'_1>=9000 & code`x'_1<=9420
	replace occ_group`x' = 23 if code`x'_1>=9500 & code`x'_1<=9750
	replace occ_group`x' = 24 if code`x'_1>=9800 & code`x'_1<=9830
	replace occ_group`x' = 25 if code`x'_1==11111
	replace occ_group`x' = 26 if code`x'_1==22222
	replace occ_group`x' = 27 if code`x'_1==33333
	replace occ_group`x' = 28 if code`x'_1==44444
	replace occ_group`x' = 29 if code`x'_1==55555
	replace occ_group`x' = 30 if code`x'_1==66666
	replace occ_group`x' = 31 if code`x'_1==77777
	replace occ_group`x' = 32 if code`x'_1==88888
	replace occ_group`x' = 33 if code`x'_1==99999
	
	label val occ_group`x' occ_group
	
}

* Additional modifications from manual classification
forval x=1995/2014 {
	
	replace occ_group`x' = 8 	if code_orig`x'_1==3
	replace occ_group`x' = 12 	if code_orig`x'_1==5
	replace occ_group`x' = 2 	if code_orig`x'_1==9
	replace occ_group`x' = 10 	if code_orig`x'_1==10
	replace occ_group`x' = 18 	if code_orig`x'_1==12

}

* For parents, see if there is another occupation listed
forval x=1995/2014 {
	
	gen c = 0
	
	forval y = 2/3 {
	
		replace occ_group`x' = 1 if code`x'_`y'>=10 & code`x'_`y'<=430 		& code`x'_1==44444 & c==0
		replace occ_group`x' = 2 if code`x'_`y'>=500 & code`x'_`y'<=950  	& code`x'_1==44444 & c==0
		replace occ_group`x' = 3 if code`x'_`y'>=1000 & code`x'_`y'<=1240  	& code`x'_1==44444 & c==0
		replace occ_group`x' = 4 if code`x'_`y'>=1300 & code`x'_`y'<=1560  	& code`x'_1==44444 & c==0
		replace occ_group`x' = 5 if code`x'_`y'>=1600 & code`x'_`y'<=1965  	& code`x'_1==44444 & c==0
		replace occ_group`x' = 6 if code`x'_`y'>=2000 & code`x'_`y'<=2060  	& code`x'_1==44444 & c==0
		replace occ_group`x' = 7 if code`x'_`y'>=2100 & code`x'_`y'<=2160  	& code`x'_1==44444 & c==0
		replace occ_group`x' = 8 if code`x'_`y'>=2200 & code`x'_`y'<=2550  	& code`x'_1==44444 & c==0
		replace occ_group`x' = 9 if code`x'_`y'>=2600 & code`x'_`y'<=2960  	& code`x'_1==44444 & c==0
		replace occ_group`x' = 10 if code`x'_`y'>=3000 & code`x'_`y'<=3540  & code`x'_1==44444 & c==0
		replace occ_group`x' = 11 if code`x'_`y'>=3600 & code`x'_`y'<=3655  & code`x'_1==44444 & c==0
		replace occ_group`x' = 12 if code`x'_`y'>=3700 & code`x'_`y'<=3955  & code`x'_1==44444 & c==0
		replace occ_group`x' = 13 if code`x'_`y'>=4000 & code`x'_`y'<=4160  & code`x'_1==44444 & c==0
		replace occ_group`x' = 14 if code`x'_`y'>=4200 & code`x'_`y'<=4250  & code`x'_1==44444 & c==0
		replace occ_group`x' = 15 if code`x'_`y'>=4300 & code`x'_`y'<=4650  & code`x'_1==44444 & c==0
		replace occ_group`x' = 16 if code`x'_`y'>=4700 & code`x'_`y'<=4965  & code`x'_1==44444 & c==0
		replace occ_group`x' = 17 if code`x'_`y'>=5000 & code`x'_`y'<=5940  & code`x'_1==44444 & c==0
		replace occ_group`x' = 18 if code`x'_`y'>=6005 & code`x'_`y'<=6130  & code`x'_1==44444 & c==0
		replace occ_group`x' = 19 if code`x'_`y'>=6200 & code`x'_`y'<=6940  & code`x'_1==44444 & c==0
		replace occ_group`x' = 20 if code`x'_`y'>=7000 & code`x'_`y'<=7630  & code`x'_1==44444 & c==0
		replace occ_group`x' = 21 if code`x'_`y'>=7700 & code`x'_`y'<=8965  & code`x'_1==44444 & c==0
		replace occ_group`x' = 22 if code`x'_`y'>=9000 & code`x'_`y'<=9420  & code`x'_1==44444 & c==0
		replace occ_group`x' = 23 if code`x'_`y'>=9500 & code`x'_`y'<=9750  & code`x'_1==44444 & c==0
		replace occ_group`x' = 24 if code`x'_`y'>=9800 & code`x'_`y'<=9830  & code`x'_1==44444 & c==0
		replace occ_group`x' = 25 if code`x'_`y'==11111  & code`x'_1==44444 & c==0
		replace occ_group`x' = 26 if code`x'_`y'==22222  & code`x'_1==44444 & c==0
		replace occ_group`x' = 27 if code`x'_`y'==33333  & code`x'_1==44444 & c==0
		
		replace occ_group`x' = 29 if code`x'_`y'==55555  & code`x'_1==44444 & c==0
		replace occ_group`x' = 30 if code`x'_`y'==66666  & code`x'_1==44444 & c==0
		replace occ_group`x' = 31 if code`x'_`y'==77777  & code`x'_1==44444 & c==0
		replace occ_group`x' = 32 if code`x'_`y'==88888  & code`x'_1==44444 & c==0
		replace occ_group`x' = 33 if code`x'_`y'==99999  & code`x'_1==44444 & c==0
		
		* Additional modifications from manual classification	
		replace occ_group`x' = 8 	if code_orig`x'_`y'==3 & code`x'_1==44444 & c==0
		replace occ_group`x' = 12 	if code_orig`x'_`y'==5 & code`x'_1==44444 & c==0
		replace occ_group`x' = 2 	if code_orig`x'_`y'==9 & code`x'_1==44444 & c==0
		replace occ_group`x' = 10 	if code_orig`x'_`y'==10 & code`x'_1==44444 & c==0
		replace occ_group`x' = 18 	if code_orig`x'_`y'==12 & code`x'_1==44444 & c==0
		
		replace c = 1 if code`x'_1==44444 & occ_group`x'!=28 

	}
	
	drop c
}

* See if officeholder status is listed in second/third occupations
forval x=1995/2014 {
	
	forval y = 2/3 {
		replace occ_group`x' = 31 if code`x'_`y'==77777
	}
}

rm temp.dta 
